package com.yidiandao.business.ui.main.shoppingcart.chooseredpacket;

import com.yidiandao.business.data.bean.Coupon;
import com.yidiandao.business.data.model.ICouponModel;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseRedPacketListItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;

/**
 * 选择红包页面 - Presenter
 */
public class ChooseRedPacketPresenter implements ChooseRedPacketContract.Presenter {

    private ChooseRedPacketContract.View mChooseRedPacketView;

    /**
     * 可选择的红包列表
     */
    private List<Coupon> mRedPackets;

    private Coupon mChooseRedPacket;

    private boolean mUseRedPacket;

    private ICouponModel mCouponModel;

    public ChooseRedPacketPresenter(ChooseRedPacketContract.View view, ICouponModel couponModel) {
        mChooseRedPacketView = view;
        mCouponModel = couponModel;

        mChooseRedPacketView.setPresenter(this);
    }

    @Override
    public void start() {
        mCouponModel.getCartRedPacketList(mChooseRedPacketView.getPagerTag(), new ICouponModel.OnCartUsefulCouponListener() {
            @Override
            public void onSuccess(List<Coupon> source, List<DisplayListItem> displayListItems, Coupon selected) {

                // 保存选中的红包
                // 注意:可能为null --> 未选择红包
                mChooseRedPacket = selected;
                mUseRedPacket = (selected != null);
                // 状态回显
                mChooseRedPacketView.refreshUseRedPacketState(mUseRedPacket);
                mChooseRedPacketView.setListViewVisibility(mUseRedPacket);

                mRedPackets = source;
                mChooseRedPacketView.refreshContent(mChooseRedPacketView.checkResultObject(source));
                mChooseRedPacketView.setDisplayListItems(displayListItems);
            }

            @Override
            public void onFailed() {
                mChooseRedPacketView.refreshContent(SuperLoadingLayout.STATE_ERROR);
            }
        });
    }

    @Override
    public void onItemClick(ChooseRedPacketListItem listItem) {

        // 使用红包
        mUseRedPacket = true;

        // 设置选中当前的红包
        mChooseRedPacket = listItem.getContentData();
        mChooseRedPacket.setIsSelected(1);

        for (Coupon other : mRedPackets) {
            if (other != mChooseRedPacket) {
                other.setIsSelected(0);
            }
        }

        mChooseRedPacketView.notifyDataSetChanged();
    }

    @Override
    public void onConfirmChoose() {

        // 选择红包
        String paperId;
        if (mUseRedPacket) {
            paperId = mChooseRedPacket == null ? "0" : mChooseRedPacket.getPaperId();
        } else {
            paperId = "0";
        }

        mCouponModel.chooseRedPacket(mChooseRedPacketView.getPagerTag(), paperId, new ICouponModel.OnAccessNetListener() {
            @Override
            public void onSuccess(List<DisplayListItem> displayListItems, boolean emptyResult, boolean hasMore, String totalItem) {
                mChooseRedPacketView.finishActivity();
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void setUseRedPacket(boolean useRedPacket) {
        mUseRedPacket = useRedPacket;
    }
}
